package com.yljk.yljkproject.utils;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;

import com.yilijk.base.utils.ALog;

public class AppMarketUtil {

    private static final String MARKET_XIAOMI = "com.xiaomi.market";
    private static final String MARKET_HUAWEI = "com.huawei.appmarket";
    private static final String MARKET_VIVO = "com.bbk.appstore";
    private static final String MARKET_OPPO = "com.oppo.market";
//    private static final String MARKET_OPPO = "com.heytap.market";

    private static AppMarketUtil util;

    private AppMarketUtil() {
    }

    public static AppMarketUtil getInstance() {
        if (util == null) {
            synchronized (AppMarketUtil.class) {
                if (util == null) {
                    util = new AppMarketUtil();
                }
            }
        }
        return util;
    }


    public void callXiaomiAppDetail(String pbkName, Context context, JumpListener listener) {
        callAppDetail(pbkName, context, MARKET_XIAOMI, listener);
    }

    public void callHuaweiAppDetail(String pbkName, Context context, JumpListener listener) {
        callAppDetail(pbkName, context, MARKET_HUAWEI, listener);
    }

    public void callVivoAppDetail(String pbkName, Context context, JumpListener listener) {
//        callAppDetail(pbkName, context, MARKET_VIVO);
        jumpVivoAppDetail(pbkName, context, listener);
    }

    public void callOppoAppDetail(String pbkName, Context context, JumpListener listener) {
        callAppDetail(pbkName, context, MARKET_OPPO, listener);
    }

    /**
     * 跳转到商店详情页自动更新，请按照商店版本要求自行控制调用此方法的前置条件
     *
     * @param context 上下文
     */
    private void jumpVivoAppDetail(String pkgName, Context context, JumpListener listener) {
        Intent intent = new Intent();
        String url = "vivomarket://details?id=" + pkgName + "&th_name=self_update";
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        intent.setPackage("com.bbk.appstore");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                Intent.FLAG_ACTIVITY_CLEAR_TOP);
        try {
            context.startActivity(intent);
        } catch (Throwable throwable) {
            if (listener != null) {
                listener.onThrowable(throwable);
            }
            ALog.e("oakkk", "callVivoAppDetail" + " activity not found!");
        }
    }

    private void callAppDetail(String pbkName, Context context, String marketName, JumpListener listener) {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        String url = "market://details?id=" + pbkName;
        Uri uri = Uri.parse(url);
        intent.setData(uri);
        //指定应用市场接收
        intent.setPackage(marketName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (Throwable throwable) {
            if (listener != null) {
                listener.onThrowable(throwable);
            }
            ALog.e("oakkk", "callAppDetail" + marketName + " activity not found!");
        }
    }

    private void callHuaweiAppDetail2(String pbkName, Context context) {
        Intent intent = new Intent();
        intent.setAction("com.huawei.appmarket.intent.action.AppDetail");
        // 打开指定包名详情
        intent.putExtra("APP_PACKAGENAME", pbkName);
        // 指定应用市场接收
        intent.setPackage("com.huawei.appmarket");
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (Throwable throwable) {
            ALog.e("oakkk", "callHuaweiAppDetail activity not found!");
        }
    }

    private void callHuaweiAppDetail3(String pbkName, Context context) {
        Intent intent = new Intent("com.huawei.appmarket.appmarket.intent.action.AppDetail.withdetailId");
        intent.setPackage("com.huawei.appmarket");
        intent.putExtra("appDetailId", "package|" + pbkName);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            context.startActivity(intent);
        } catch (Throwable throwable) {
            ALog.e("oakkk", "callHuaweiAppDetail3 activity not found!");
        }
    }

    public interface JumpListener {
        void onThrowable(Throwable throwable);
    }

}
